﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using CsQuery;
using RedFoxUtils;

namespace QSMonitor
{
    public partial class frmRate : Form
    {
        DataTable rateDT = null;
        public frmRate()
        {
            InitializeComponent();
            ucBrowser1.NavigateURL(
@"https://ebank.taipeifubon.com.tw/ibank/servlet/HttpDispatcher/ExRateToday/query");
            //
            /*
            createTable();
            
            //processRate();
            timer1.Start();
             * */
        }

        void createTable()
        {
            rateDT = new DataTable();
            DataColumn col = new DataColumn();
            col.ColumnName = "日期時間";
            col.DataType = typeof(System.DateTime);
            rateDT.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "銀行";
            col.DataType = typeof(System.String);
            rateDT.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "即期買";
            col.DataType = typeof(System.Decimal);
            rateDT.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "即期賣";
            col.DataType = typeof(System.Decimal);
            rateDT.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "現金買";
            col.DataType = typeof(System.Decimal);
            rateDT.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "現金賣";
            col.DataType = typeof(System.Decimal);
            rateDT.Columns.Add(col);           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            processRate();
        }

        void processRate()
        {
            string webaddress = "https://ebank.taipeifubon.com.tw/ibank/servlet/HttpDispatcher/ExRateToday/query";
            
            try
            {
            
                WebClient wc = new WebClient();
                wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                byte[] responseData = wc.DownloadData(webaddress);
                string srcString = Encoding.GetEncoding("big5").GetString(responseData);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            /* */

            
            string msg = string.Empty;
            HttpClient client = new HttpClient();
            string doctxt = client.GetSrc(webaddress, "big5", out msg);
            client.CQCreateFromUrl(webaddress);
            var dom = CQ.Create(doctxt);
            var td = dom.Select(".main-text");
            MessageBox.Show(td[12].InnerText);


            /*
            string srcString = Encoding.GetEncoding("big5").GetString(Encoding.Default.GetBytes(ucBrowser1.DocumentText()));
            string doctxt = Encoding.UTF8.GetString(Encoding.Convert(Encoding.GetEncoding("big5"), Encoding.UTF8
                , Encoding.UTF8.GetBytes(ucBrowser1.DocumentText())));
            var dom = CQ.Create(doctxt);
            var td = dom.Select(".main-text");
            string ddd = Encoding.UTF8.GetString(Encoding.Convert(Encoding.GetEncoding("big5"), Encoding.UTF8
                ,Encoding.Default.GetBytes(td[0].InnerText)));

            //var dom = CQ.CreateFromUrl("https://ebank.taipeifubon.com.tw/ibank/servlet/HttpDispatcher/ExRateToday/query");
            //HtmlDocument dom = ucBrowser1.get

            
             * WebClient client = new WebClient();
            System.IO.Stream stre = client.OpenRead("https://ebank.taipeifubon.com.tw/ibank/servlet/HttpDispatcher/ExRateToday/query");
            WebClient client = new WebClient();
            client.Headers["User-Agent"] =
        "Googlebot/2.1 (+http://www.googlebot.com/bot.html)";
            byte[] arr = client.DownloadData("http://tw.yahoo.com/");
            string srcString = Encoding.GetEncoding("big5").GetString(arr);

            var dom = CQ.Create(srcString);
            */
        }

        void processRate1()
        {
            /*
            HtmlElementCollection opts = ucBrowser1.GetElementsByTag("option");
            List<HtmlElement> sele = ucBrowser1.GetElementByName("select", "name", new string[] { "cur" });
            List<HtmlElement> trs = null;
            foreach (HtmlElement opt in opts)
            {                
                if (opt.InnerText.Equals("日圓(JPY)"))
                {
                    if (opt.GetAttribute("selected").Equals("True"))
                    {
                        //		      <input type="button" name="submitB1" value="更新即時匯率" class="main-bottum" onClick="javascript:onSubmit();">&nbsp;
                        List<HtmlElement> btns = ucBrowser1.GetElementByName("input", "name", new string[] { "submitB1" });
                        ucBrowser1.ElementInvok(btns[0], "click");
                    }
                    else
                    {
                        opt.SetAttribute("selected", "true");
                        ucBrowser1.ElementInvok(sele[0], "onChange");                    
                    }
                    trs = ucBrowser1.GetElementByName(
    "tr", "bgcolor", new string[] { "#FFFFFF" });

                    break;
                }
            }
            
            if (trs == null)
            { return; }
            HtmlElementCollection tds1,tds2;
            tds1 = trs[0].GetElementsByTagName("td");
            tds2 = trs[1].GetElementsByTagName("td");
            int[] td1 = { 2,9};
            int[] td2 = { 1,8};

            DataRow row = rateDT.NewRow();
            row[0] = DateTime.Now;
            row[1] = "Fubon";
            row[2] = Convert.ToDecimal(tds1[2].InnerText.ToString());
            row[3] = Convert.ToDecimal(tds1[9].InnerText.ToString());
            row[4] = Convert.ToDecimal(tds2[1].InnerText.ToString());
            row[5] = Convert.ToDecimal(tds2[8].InnerText.ToString());
            rateDT.Rows.Add(row);

            ucGrid1.SetData(rateDT);
             */
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            processRate();
            timer1.Start();
        }
    }
}
